package com.example.archive.controller;

import com.example.archive.entity.User;
import com.example.archive.service.UserService;
import com.example.archive.util.Result;
import com.example.archive.dto.LoginResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody User user) {
       return userService.register(user);
    }

    @PostMapping("/login")
    public Result<LoginResponseDto> login(@RequestParam String username, @RequestParam String password) {
        return userService.login(username, password);
    }

    @PostMapping("/logout")
    public Result<Boolean> logout(@RequestHeader("token") String token) {
        // Token is now sent directly in 'token' header, no 'Bearer ' prefix expected
        return userService.logout(token);
    }

    @GetMapping("/list")
    public Result<List<User>> listUsers(String search) {
        return userService.listUsers( search);
    }

    @PostMapping("/add")
    public Result<Boolean> addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @PutMapping("/update")
    public Result<Boolean> updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @DeleteMapping("/delete/{userId}")
    public Result<Boolean> deleteUser(@PathVariable Integer userId) {
        return userService.deleteUser(userId);
    }
} 